<?php
if(!isset($MAIN)){
	include_once("includes/class.ibnfm.php");
	$MAIN = new MAIN();
}
$MAIN->users->checkLogin(true);

if(isset($_REQUEST['search'])){
	$associations = $MAIN->associations->searchAssociations($_REQUEST['search']);
	foreach($associations as $as){
	?>
	<div onclick="fillForm('associations',<?php echo($as['id']);?>);" id="assoc_<?php echo($as['id']);?>"><?php echo($as['name']);?></div>
	<?php
	}
	?>
	<script type="text/javascript">
	var assoc={};
	<?php	
	foreach($associations as $as){
	?>
	assoc[<?php echo($as['id']);?>]=<?php echo(json_encode($as));?>;	 
	<?php
	}
	?>
	</script>
	<?php
	exit();
}

if(isset($_POST['txt_association_name'])){
	print_r($_POST);
	if($asId = $MAIN->associations->setAssociation($_POST['txt_association_id'],$_POST)){
	?>
	<script type="text/javascript">sendInfo(null,'Dina ändringar har sparats!');(function(){document.location.href='index.php?cat=associations&view=all';}).delay(3000);</script>
	<?php
	}
	exit();
}
if(isset($_REQUEST['del'])){
	if($MAIN->associations->delAssociation($_REQUEST['del'])){
	?>
	<script type="text/javascript">sendInfo(null,'Dina ändringar har sparats!');(function(){document.location.href='index.php?cat=associations&view=all';}).delay(3000);</script>
	<?php	
	};
}
?>
<div class="activ">
<?php
switch(@$_REQUEST['view']){
	default:
	case 'all':
	$ord="ASC";
	$orderBy="name";
	if(isset($_REQUEST['ordby'])){$orderBy=$_REQUEST['ordby'];}
	$nextOrd="DESC";
	if(isset($_REQUEST['ord'])){$ord=$_REQUEST['ord'];$nextOrd =($_REQUEST['ord']=="ASC")?"DESC":"ASC";}
	$associations = $MAIN->associations->getAssociations($orderBy,$ord);
?>
	<h1>Alla registrerade föreningar</h1>
	<table cellpadding="0" cellspacing="0" class="list">
	<tr>		
		<th><a href="?cat=associations&amp;view=all&amp;ordby=name&amp;ord=<?php echo($nextOrd);?>">Namn</a></th>
		<th style="width:10px;"></th>
		<th><a href="?cat=associations&amp;view=all&amp;ordby=lastname&amp;ord=<?php echo($nextOrd);?>">Adress</a></th>
		<th style="width:10px;"></th>
		<th><a href="?cat=associations&amp;view=all&amp;ordby=email&amp;ord=<?php echo($nextOrd);?>">Postnummer</a></th>
		<th style="width:10px;"></th>		
		<th><a href="?cat=associations&amp;view=all&amp;ordby=pers&amp;ord=<?php echo($nextOrd);?>">Ort</a></th>
		<th style="width:10px;"></th>		
		<th><a href="?cat=associations&amp;view=all&amp;ordby=status&amp;ord=<?php echo($nextOrd);?>">Status</a></th>
		<th style="width:10px;"></th>		
		<th>Administrera</th>
		<th style="width:10px;"></th>
		<th style=" text-align:right;"><a href="?cat=associations&amp;view=all&amp;ordby=modif&amp;ord=<?php echo($nextOrd);?>">Senaste ändring</a></th>
	</tr>
	<?php
	foreach($associations as $as){
	?>
	<tr class="entry">		
		<td><?php echo($as['name']);?></td>
		<td></td>
		<td><?php echo($as['address']);?></td>
		<td></td>
		<td><?php echo($as['zip']);?></td>
		<td></td>
		<td><?php echo($as['city']);?></td>
		<td></td>
		<td><?php echo($as['status']);?></td>
		<td></td>
		<td class="tools" style="text-align:left;">
			<?php
			if($MAIN->currentUser['grouplevel']<=50){
			?>
			<a href="javascript:void(0);" onclick="delAssociation(<?php echo($as['id']);?>)"><img src="images/btn_admin_del.png" alt="Ta Bort" title="Ta Bort"/></a>			
			<a href="?cat=associations&amp;view=edit&amp;id=<?php echo($as['id']);?>"><img src="images/btn_admin_edit.png" alt="Redigera" title="Redigera"/></a>
			<?php
			}
			?>
		</td>
		<td></td>
		<td style="text-align:right;"><?php echo(date('Y-m-d',strtotime($as['modif'])));?></td>
	</tr>
	<?php
	}
	?>
	</table>
	<script type="text/javascript">
		var delAssociation = function(id){
			var msg='Bekräfta borttagning';
			if(confirm(msg)){
			ajaxGet('associations.php','del='+id);
			}
		}
	</script>
<?php
	break;
	case 'add':
	case 'edit':
		$as = $MAIN->associations->getAssociation(@$_REQUEST['id']);
?>
	<h1>Skapa/Redigera Förening</h1>
	<table cellpadding="0" cellspacing="0">
		<tr>
			<td>
				<form name="frm_association" id="frm_association" action="associations.php" method="post" onsubmit="return false;">
				<input type="hidden" name="txt_association_id" value="<?php echo($as['id']);?>"/>	
				<table cellpadding="0" cellspacing="0">
					<tr>
						<td>Föreningens namn</td>
						<td class="right"><input class="required" type="text" name="txt_association_name" value="<?php echo(@$as['name']);?>"/></td>			
					</tr>
					<tr>
						<td>Organisationsnummer</td>
						<td class="right"><input class="required validate-org" type="text" name="txt_association_org" value="<?php echo(@$as['org']);?>"/></td>
					</tr>
					<tr>
						<td>Föreningens adress</td>
						<td class="right"><input class="required" type="text" name="txt_association_address" value="<?php echo(@$as['address']);?>"/></td>
					</tr>
					<tr>
						<td>Postnummer</td>
						<td class="right"><input class="required" type="text" name="txt_association_zip" value="<?php echo(@$as['zip']);?>"/></td>
					</tr>
					<tr>
						<td>Ort</td>
						<td class="right"><input class="required" type="text" name="txt_association_city" value="<?php echo(@$as['city']);?>"/></td>
					</tr>
					<tr>
						<td>Medlem i :</td>
						<td>
							<input type="hidden" name="ddl_association_member_of" id="ddl_association_member_of" value="<?php echo(@$as['member_of']);?>"/>
							<div id="association_member_of_list" style="">
							<?php
							$mmemberof = explode(',',$as['member_of']);
							$membersof=$MAIN->returnArray("select * from `".tblPrefix."memberof`");
							foreach($membersof as $m){
							?>
							<div>
							<input style="margin-bottom:-3px;" <?php if(in_array($m['id'],$mmemberof)){?>checked="checked"<?php }?> type="checkbox" name="chk_<?php echo($m['id']);?>" id="chk_<?php echo($m['id']);?>" value="<?php echo($m['id']);?>"/>&nbsp;&nbsp;<label for="chk_<?php echo($m['id']);?>"><?php echo($m['name']);?></label>
							</div>
							<?php
							}
							?>
							</div>
						</td>
					</tr>
					<tr>
						<td>Status</td>
						<td class="right">
							<select name="ddl_association_status" style="margin:3px 0px;">
								<option value="Medlem">Medlem</option>
								<option value="Samarbetspartner">Samarbetspartner</option>
								<option value="Annat">Annat</option>
							</select>
						</td>
					</tr>
					<tr>
						<td>Studiekonsulent<br/>administratör :</td>
						<td class="right">
							<?php
							$consults = $MAIN->users->getUsers();											
							?>
							<select name="ddl_association_owned_by" style="margin:3px 0px;" value="0">
								<?php
								if($MAIN->currentUser['grouplevel']==50){
									$consults="";
									$consults[0]=$MAIN->currentUser;
									?>
								<option selected="selected" value="<?php echo($consults[0]['id']);?>"><?php echo($consults[0]['email']);?></option>	
									<?php								
								}else{
								?>
								<option value="0">-- ange användare --</option>
							<?php
							foreach ($consults as $user) {
								if($user['grouplevel']==50){
							?>
								<option <?php if($user['id']==$as['owned_by']){?> selected="selected"<?php }?> value="<?php echo($user['id']);?>"><?php echo($user['email']);?></option>
							<?php	
								}								
							}
								}
							?>					
							</select>
						</td>			
					</tr>
					<tr>
						<td colspan="2" class="right">
							<input type="submit" name="btn_submit" value="Spara" onclick="sendForm('frm_association');"/>
						</td>
					</tr>
				</table>	
				</form>
				<script type="text/javascript">
				var chks = $('association_member_of_list').getElements('input');
				chks.addEvent('change', function(){
					var txt=[];
					chks.each(function(input,i){
						if(input.checked){
							txt.push(input.value);
						}
					})
					$('ddl_association_member_of').value=txt;
				});
				</script>
			</td>
			<td>
				<div class="contact_person">
					<?php
					if($as['owned_by']!=0){
					$contact = $MAIN->users->getUser($as['owned_by']);
					?>
					<h2 style="text-align:right;">Kontaktperson/Studiekonsulent</h2>
					<div id="contact_person">
						<a href="mailto:<?php echo($contact['email']);?>"><?php echo($contact['email']);?></a><br/>
						<?php if($contact['person_id']!="" || $contact['person_id']!=0){ ?>						
						<?php echo($contact['firstname']." ".$contact['lastname']);?><br/>
						<?php echo($contact['address']);?><br/>
						<?php echo($contact['zip']." ".$contact['city']);?><br/>
						<?php echo($contact['tel']);?><br/>
						<?php } ?>
					</div>
					<hr/>
					<?php					
					}
					?>
				</div>
				<div class="study_circles">
					<?php
					$ass_circles = $MAIN->associations->getAssociationCircles($as['id']);
					if(count($ass_circles)>0){
					?>
					<h2 style="text-align:right;">Studiecirklar i denna förening</h2>
					<?php					
						foreach($ass_circles as $circle){
						?>
						<span><a href="?cat=circles&amp;view=edit&amp;id=<?php echo($circle['id']);?>"><?php echo($circle['name']);?></span></a><br/>
						<?php
						}
					?>
					<hr/>
					<?php
					}
					?>
				</div>				
				<div class="notes">
				<?php 
				if(isset($_REQUEST['id']) && $_REQUEST['id']!=0){
				?>
				<h2 style="text-align:right;">Allmänna notiser</h2>
				<div class="note" style="width:410px; text-align:right; float:right;">
					<textarea name="txt_note" id="txt_note"></textarea>
					<div class="tools" style="padding:0px;">
						<input type="submit" name="btn_submit" value="Spara" onclick="ajaxGet('note.php','rel_table=associations&id=<?php echo($as['id']);?>&text='+$('txt_note').value);$('txt_note').value='';"/>
					</div><br/>
				</div>				
				<div class="cnt_notes" id="cnt_notes">
				</div>
				<script type="text/javascript">
				ajaxGet('notes.php','rel_table=associations&rel_id=<?php echo($as['id']);?>',$('cnt_notes'));
				</script>
				<?php }?>
				</div>
			</td>
		</tr>
	</table>
	
	
	
<?php
	break;
}
?>
</div>